package com.example.service.impl;

import com.example.mapper.BrandMapper;
import com.example.pojo.Brand;
import com.example.pojo.PageResult;
import com.example.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    private BrandMapper brandMapper;

    public List<Brand> findAll(){
        return brandMapper.findAll();
    }

    @Override
    public PageResult page(Integer page, Integer pageSize){

        //设置分页参数
        PageHelper.startPage(page, pageSize);

        //执行查询
        List<Brand> list = brandMapper.findAll();
        Page<Brand> p = (Page<Brand>) list;

        //封装分页结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    //新增
    public void save(Brand brand){
        brand.setCreateTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        brandMapper.insert(brand);
        //重置自增主键
        //brandMapper.resetAutoIncrement();
    }

    //删除
    public void deleteById(Integer brandID){
        brandMapper.deleteById(brandID);
        //重置自增主键
        //brandMapper.resetAutoIncrement();
        // 将brandID大于指定值的记录的brandID减1
        //brandMapper.decrementBrandIDAfter(brandID);
    }


    //根据ID查询
    public Brand getById(Integer brandID){
        return brandMapper.getById(brandID);
    }

    //修改
    public void update(Brand brand){
        brand.setUpdateTime(LocalDateTime.now());
        brandMapper.update(brand);
    }

    //获取所有品牌名称
    public List<String> getBrandNames() {
        return brandMapper.getBrandNames();
    }
}
